#include "ts06.h"
#include "delay.h"

u8 __xdata ts06_ctl_reg[] = {
    0x33, //0x00:Reg_Sensitivity1    SEN_CH2[7:4] & SEN_CH1[3:0] Sensitivity
    0x33, //0x01:Reg_Sensitivity2    SEN_CH4[7:4] & SEN_CH3[7:4] Sensitivity
    0x33, //0x02:Reg_Sensitivity3    SEN_CH6[7:4] & SEN_CH4[7:4] Sensitivity
    0x0A, //0x03:Reg_CTRL1           MS[5]:auto, FTC[4:3]:10s, RTC[2:0]: 4
    0x13, //0x04:Reg_CTRL2           LED_EN[6]:Disable, S/M_Sel[5]:Multiple,     
          //                    IMP_Sel[4]:High_Impedance, SRST[3]:Disable, SLEEP[2]:Disable
    0x38, //0x05:Reg_Ref_Rst         CH6[5]:Enable, CH5[4]:Enable, CH4[3]:Enable, 
          //                    CH3[2]:Disable, CH2[1]:Disable, CH1[0]:Disable
    0x38, //0x06:Reg_CH_Hold         CH6[5]:Hold, CH5[4]:Hold, CH4[3]:Hold, 
          //                    CH3[2]:Enable, CH2[1]:Enable, CH1[0]:Always Enable(0)
    0x00, //0x07:Reg_Cal_Hold        CHn[5:0]:Enable
    0x00, //0x08:Reg_Tack_Key_En     CHn[4:0]:Disable
    0xFD, //0x09:Reg_Cal_Ctrl        BF_UP[7:6]:Slow, BF_DOWN[5:4]:Slow, BS[3:2]:Slow, SLEEP[1:0]:Normal
    0x00, //0x0A:Reg_Pat_Ctrl        
    0x00, //0x0B:Reg_Pat_A0
    0x00, //0x0C:Reg_Pat_A1
    0x00, //0x0D:Reg_Pat_A2
    0x00, //0x0E:Reg_Pat_A3
    0x00, //0x0F:Reg_Pat_B0
    0x00, //0x10:Reg_Pat_B1
    0x00, //0x11:Reg_Pat_B2
    0x00, //0x12:Reg_Pat_B3
    0x00, //0x13:Reg_PWM0
    0x00, //0x14:Reg_PWM1
    0x00, //0x15:Reg_PWM2
};

void ts06_gpio_init(void)
{
    GPIO_InitTypeDef __xdata GPIO_InitStructure;                        //结构定义
    GPIO_InitStructure.Pin = I2C2_SDA_GPIO_PIN;                 //指定要初始化的IO,
    GPIO_InitStructure.Mode = GPIO_OUT_OD;                      //指定IO的输入或输出方式
    GPIO_Inilize(I2C2_SDA_GPIO_PORT, &GPIO_InitStructure);      //初始化
    GPIO_InitStructure.Pin = I2C2_SCK_GPIO_PIN;                 //指定要初始化的IO,
    GPIO_InitStructure.Mode = GPIO_OUT_OD;                      //指定IO的输入或输出方式
    GPIO_Inilize(I2C2_SCK_GPIO_PORT, &GPIO_InitStructure);      //初始化
    // GPIO_InitStructure.Pin = TS06_INT_GPIO_PIN;                 //指定要初始化的IO,
    // GPIO_InitStructure.Mode = GPIO_HighZ;                       //指定IO的输入或输出方式
    // GPIO_Inilize(TS06_INT_GPIO_PORT, &GPIO_InitStructure);      //初始化
    I2C2_SDA_HIGH();
    I2C2_SCK_HIGH();
}

static void i2c_start(void)
{
    I2C2_SCK_HIGH();
    I2C2_SDA_HIGH();
    delay_ms(1);
    I2C2_SDA_LOW();
    delay_ms(1);
    I2C2_SCK_LOW();
    delay_ms(1);
}

static void i2c_send_byte(u8 byte)
{
    I2C2_SCK_LOW();
    for(u8 i = 0; i < 8; i++)
    {
        if(byte & 0x01)
        {
            I2C2_SDA_HIGH();
        }
        else
        {
            I2C2_SDA_LOW();
        }
            
        byte >>= 1;
        I2C2_SCK_HIGH();
        delay_ms(1);
        I2C2_SCK_LOW();
        delay_ms(1);
    }
}

static u8 i2c_recv_byte(void)
{
    u8 __xdata byte = 0;
    I2C2_SCK_LOW();
    I2C2_SDA_HIGH();
    delay_ms(1);
    for(u8 i = 0; i < 8; i++)
    {
        I2C2_SCK_HIGH();
        delay_ms(1);
        byte >>= 1;
        if(I2C2_SDA_READ())
        {
            byte |= 0x80;
        }
        I2C2_SCK_LOW();
        delay_ms(1);
    }
    return byte;
}

static void i2c_stop(void)
{
    I2C2_SCK_LOW();
    I2C2_SDA_LOW();
    delay_ms(1);
    I2C2_SCK_HIGH();
    delay_ms(1);
    I2C2_SDA_HIGH();
    delay_ms(1);
}

static u8 i2c_wait_ack(void)
{
    u8 __xdata ret = 1;
    I2C2_SDA_HIGH();
    I2C2_SCK_LOW();
    delay_ms(1);
    I2C2_SCK_HIGH();
    delay_ms(1);
    if(I2C2_SDA_READ() == 0)
    {
        ret = 0;
    }
    I2C2_SCK_LOW();
    return ret;
}

static void i2c_ack(void)
{
    I2C2_SCK_LOW();
    I2C2_SDA_LOW();
    delay_ms(1);
    I2C2_SCK_HIGH();
    delay_ms(1);
    I2C2_SCK_LOW();
}

static void i2c_nack(void)
{
    I2C2_SCK_LOW();
    I2C2_SDA_HIGH();
    delay_ms(1);
    I2C2_SCK_HIGH();
    delay_ms(1);
    I2C2_SCK_LOW();
}

void ts06_write_data(u8 addr, u8 *buf, u16 len)
{
    i2c_start();
    i2c_send_byte(TS06_WRITE_OPS);
    i2c_wait_ack();
    i2c_send_byte(addr);
    i2c_wait_ack();
    while(len--)
    {
        i2c_send_byte(*buf++);
        i2c_wait_ack();
    }
    i2c_stop();
}

void ts06_read_data(u8 addr, u8 *buf, u16 len)
{
    i2c_start();
    i2c_send_byte(TS06_WRITE_OPS);
    i2c_wait_ack();
    i2c_send_byte(addr);
    i2c_wait_ack();
    i2c_stop();
    i2c_start();
    i2c_send_byte(TS06_READ_OPS);
    i2c_wait_ack();
    while(len--)
    {
        *buf++ = i2c_recv_byte();

        if(len)
            i2c_ack();
        else
            i2c_nack();
    }
    i2c_stop();
}

void ts06_sleep(void)
{
    u8 __xdata tmp = 0;
    ts06_read_data(0x04, &tmp, 1);
    tmp |= 0x04;
    ts06_write_data(0x04, &tmp, 1);
}

void ts06_reset(void)
{
    u8 __xdata tmp = 0;
    ts06_read_data(0x04, &tmp, 1);
    tmp |= 0x08;
    ts06_write_data(0x04, &tmp, 1);
}

void ts06_read_status(u8 *state)
{
    ts06_read_data(0x25, state, 1);
}

void ts06_init(void)
{
    ts06_gpio_init();
    ts06_write_data(0x00, ts06_ctl_reg, 22);
}